<?php

namespace App\Http\Controllers\Client\Course;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CourseBuyBeforeController extends ClientController
{

    /**
     * 课程购买前的查询
     */
    public function index()
    {

        $all = $this->all;
        if (!isset($all['course_id'])) {
            return $this->setReturnJson(-1, '未传参数');
        }

        try {
            // 验证是否购买了课程
            $bought = $this->checkIsBought($all['course_id']);
            if ($bought) {
                return $this->setReturnJson(-1, '您已经购买了这个课程，请重试');
            }

            // 查询是否已经绑定过手机号(未绑定手机号需要绑定手机号)
            $user_phone = DB::table('lottery_users')
                ->where('we_id', '=', $this->we_id)
                ->value('phone');
            $require_phone = $user_phone ? false : true;

            // 查询课程资源
            $source = $this->courseSources($all['course_id']);

            // 查询课程章节目录
            $contents = $this->courseContents($all['course_id']);

            return $this->setReturnJson(0, 'success', ['source' => $source, 'contents' => $contents, 'require_phone' => $require_phone,]);
        } catch (\Exception $e) {
            Log::error(CourseBuyBeforeController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);

            return $this->setReturnJson(-1, '课程价格查询失败');
        }


    }

    /**
     * 验证用户是否已经购买了课程
     * @param int $course_id
     * @return int
     */
    public function checkIsBought(int $course_id): int
    {
        $bought = DB::table('laifei_course')
            ->where('course_id', '=', $course_id)
            ->where('we_id', '=', $this->we_id)
            ->count();
        return $bought;
    }

    /**
     * 查询课程资源
     * @param int $course_id
     * @return array
     */
    public function courseSources($course_id)
    {

        $course = DB::table('laifei_sys_course')
            ->where('course_id', '=', $course_id)
            ->first();
        $course = json_decode(json_encode($course), true);

        $course['price_before'] = (int)($course['price_before'] * 100);
        $course['price'] = (int)($course['price'] * 100);
        $course['title_img'] = AliOss::getUrl($course['title_img']);
        $course['back_img'] = AliOss::getUrl($course['back_img']);

        // 是否是vip,如果是vip，课程85折
        $is_vip = DB::table('laifei_vip')
            ->where('we_id', '=', $this->we_id)
            ->where('end_time', '>', date('Y-m-d H:i:s'))
            ->where('vip_id', '=', 2)
            ->count();
        if ($is_vip) {
            $course['price'] = ceil($course['price'] * 0.85);
        }

        return $course;
    }

    /**
     * 课程章节资源
     * @param int $course_id
     * @return array
     */
    public function courseContents(int $course_id): array
    {
        // 查询课程信息
        $contents = DB::table('laifei_sys_lessons')
            ->where('course_id', '=', $course_id)
            ->select('lesson_id', 'course_id', 'title', 'subtitle')
            ->get()
            ->toArray();

        return $contents;
    }


}